Skip to content

DOC: update the DatetimeIndex.tz_convert(tz) docstring #20096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

hammadmashkoor
Copy link
Contributor

@hammadmashkoor hammadmashkoor commented Mar 10, 2018

Checklist for the pandas documentation sprint (ignore this if you are doing
an unrelated PR):

  • PR title is "DOC: update the docstring"
  • The validation script passes: scripts/validate_docstrings.py <your-function-or-method>
  • The PEP8 style check passes: git diff upstream/master -u -- "*.py" | flake8 --diff
  • The html version looks good: python doc/make.py --single <your-function-or-method>
  • It has been proofread on language by another sprint participant

Please include the output of the validation script below between the "```" ticks:

(pandas_dev) root@kalih:~/pythonpanda/pandas# python -m flake8 ./pandas/core/indexes/datetimes.py
(pandas_dev) root@kalih:~/pythonpanda/pandas# python scripts/validate_docstrings.py pandas.DatetimeIndex.tz_convert

################################################################################
################# Docstring (pandas.DatetimeIndex.tz_convert)  #################
################################################################################

Convert tz-aware DatetimeIndex from one time zone to another.

When using DatetimeIndex providing with timezone this method
converts tz(timezone)-aware DatetimeIndex from one timezone
to another.

Parameters
----------
tz : string, pytz.timezone, dateutil.tz.tzfile or None
    Time zone for time. Corresponding timestamps would be converted
    to time zone of the DatetimeIndex.
    None will remove timezone holding UTC time.

Returns
-------
normalized : DatetimeIndex

Raises
------
TypeError
    If DatetimeIndex is tz-naive.

See Also
--------
tz_localize : Localize tz-naive DatetimeIndex to given time zone,
              or remove timezone from tz-aware DatetimeIndex.

Examples
--------
With the `tz` parameter, we can change the DatetimeIndex
to other time zones:

>>> dti = pd.DatetimeIndex(start='2014-08-01 09:00',
...                       freq='H', periods=3)

>>> dti
DatetimeIndex(['2014-08-01 09:00:00',
               '2014-08-01 10:00:00',
               '2014-08-01 11:00:00'],
                dtype='datetime64[ns]', freq='H')

>>> dti.tz_localize('Europe/Berlin').tz_convert('US/Eastern')
DatetimeIndex(['2014-08-01 03:00:00-04:00',
               '2014-08-01 04:00:00-04:00',
               '2014-08-01 05:00:00-04:00'],
                dtype='datetime64[ns, US/Eastern]', freq='H')

With the `None` parameter, we can remove the timezone:

>>> dti = pd.DatetimeIndex(start='2014-08-01 09:00',freq='H',
...                        periods=3, tz='Europe/Berlin')

>>> dti
DatetimeIndex(['2014-08-01 09:00:00+02:00',
               '2014-08-01 10:00:00+02:00',
               '2014-08-01 11:00:00+02:00'],
                dtype='datetime64[ns, Europe/Berlin]', freq='H')

>>> dti.tz_convert(None)
DatetimeIndex(['2014-08-01 07:00:00',
               '2014-08-01 08:00:00',
               '2014-08-01 09:00:00'],
                dtype='datetime64[ns]', freq='H')

################################################################################
################################## Validation ##################################
################################################################################

Docstring for "pandas.DatetimeIndex.tz_convert" correct. :)

If the validation script still gives errors, but you think there is a good reason
to deviate in this case (and there are certainly such cases), please state this
explicitly.

Copy link
Contributor

@IHackPy IHackPy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try to make summary more clear
tz --> timezone

@codecov
Copy link

codecov bot commented Mar 10, 2018

Codecov Report

❗ No coverage uploaded for pull request base (master@c748de0). Click here to learn what that means.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master   #20096   +/-   ##
=========================================
  Coverage          ?   91.72%           
=========================================
  Files             ?      150           
  Lines             ?    49156           
  Branches          ?        0           
=========================================
  Hits              ?    45090           
  Misses            ?     4066           
  Partials          ?        0
Flag Coverage Δ
#multiple 90.11% <100%> (?)
#single 41.85% <100%> (?)
Impacted Files Coverage Δ
pandas/core/indexes/datetimes.py 95.64% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c748de0...ff00665. Read the comment docs.

Copy link
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Added some comments

Convert tz-aware DatetimeIndex from one time zone to another (using
pytz/dateutil)
Convert tz-aware DatetimeIndex from one
time zone to another.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you put this on one line? (I think it should fit within 80 chars)

@@ -1904,24 +1904,47 @@ def delete(self, loc):

def tz_convert(self, tz):
"""
Convert tz-aware DatetimeIndex from one time zone to another (using
pytz/dateutil)
Convert tz(timezone)-aware DatetimeIndex using pytz/dateutil.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just use 'timezone' instead of both the abbreviation and full. You can maybe put the abbreviation and explanation in the extended summary

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is also fine to leave the "using pytz/dateutil." for the extended summary, as this is already quite a detail.


Parameters
----------
tz : string, pytz.timezone, dateutil.tz.tzfile or None
Time zone for time. Corresponding timestamps would be converted to
time zone of the TimeSeries.
Time zone for time.Corresponding timestamps would be converted
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You seemed to have removed a space between the two words, which I suppose was a mistake.

None will remove timezone holding UTC time.

Returns
-------
normalized : DatetimeIndex

Raises
------
-------
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is not needed (the line under the title can be exactly as long as the title itself)

0 2018-02-28 19:00:00-05:00
1 2018-03-01 19:00:00-05:00
2 2018-03-02 19:00:00-05:00
dtype: datetime64[ns, US/Eastern]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add an example that uses tz_convert(None) ?
It's explained at the end of this section: https://pandas.pydata.org/pandas-docs/stable/timeseries.html#working-with-time-zones

Copy link
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you run the validate_docstrings script again?

Examples
--------
>>> didx = pd.DatetimeIndex
(start='2014-08-01 09:00', freq='H', periods=10, tz='Europe/Berlin')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

periods=3 is long enough I think to illustrate the example, and will give a shorter output

'2014-08-01 11:00:00-04:00', '2014-08-01 12:00:00-04:00'],
dtype='datetime64[ns, US/Eastern]', freq='H')

>>> didx.tz_convert(None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you put here a small sentence explaining the example?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jorisvandenbossche didn't realize we actually allow None here.


When using DatetimeIndex providing with timezone this method
converts tz(timezone)-aware DatetimeIndex from one timezone
to another using pytz/dateutil.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can remove the pytz/dateutil part here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's coordinate the text in this doc-string with #20050 which is sister operation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed the pytz/dateutil part

'2014-08-01 11:00:00-04:00', '2014-08-01 12:00:00-04:00'],
dtype='datetime64[ns, US/Eastern]', freq='H')

>>> didx.tz_convert(None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jorisvandenbossche didn't realize we actually allow None here.

@jreback jreback added Docs Timezones Timezone data dtype labels Mar 10, 2018
pytz/dateutil)
Convert tz-aware DatetimeIndex from one time zone to another.

When using DatetimeIndex providing with timezone this method
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a cumbersome sentence and is pretty duplicative of the above, I would remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I regret the auto typing mistake, I wanted to convey that i will surely be doing it as per recommended by you.
Please pardon my Indian English.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hammadmashkoor no problem. thanks for the PR!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this extended summary is needed at all as its duplicated of the Summary.

Time zone for time. Corresponding timestamps would be converted to
time zone of the TimeSeries.
Time zone for time. Corresponding timestamps would be converted
to time zone of the TimeSeries.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TimeSeries -> DatetimeIndex

See Also
--------
tz_localize : Localize tz-naive DatetimeIndex to given time zone
(using pytz/dateutil), or remove timezone from tz-aware
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove using pytz/dateutil

'2014-08-01 11:00:00+02:00'],
dtype='datetime64[ns, Europe/Berlin]', freq='H')

With the `tz` parameter, we can change DatetimeIndex
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change the

'2014-08-01 05:00:00-04:00'],
dtype='datetime64[ns, US/Eastern]', freq='H')

With the `None` parameter, we can remove timezone:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the timezone


Examples
--------
>>> data=pd.DatetimeIndex(start='2014-08-01 09:00',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

call this dti . (rather than data)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and spaces around the equals

With the `tz` parameter, we can change the DatetimeIndex
to other time zones:

>>> dti.tz_convert('US/Eastern')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so THIS doesn't work (well it works but doesn't do what you want).

Create 2 example dti's here, the first with NO timezone (like you had originally), and .tz_localize('Europe/Berlin')
the second with tz='Europe/Berlin' then use .tz_localize(None), this shows removing it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of tz_convert(tz), do we have to use tz_localize() ?

'2014-08-01 11:00:00'],
dtype='datetime64[ns]', freq='H')

>>> dti.tz_localize('Europe/Berlin').tz_convert('US/Eastern')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't use tz_convert here, this is about the tz_localize doc-string

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

'2014-08-01 11:00:00+02:00'],
dtype='datetime64[ns, Europe/Berlin]', freq='H')

>>> dti.tz_convert(None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes this is correct

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks..

@hammadmashkoor
Copy link
Contributor Author

made the required changes, please review @jreback

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

examples look good. some small grammer corrections.

pytz/dateutil)
Convert tz-aware DatetimeIndex from one time zone to another.

When using DatetimeIndex providing with timezone this method
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this extended summary is needed at all as its duplicated of the Summary.

Time zone for time. Corresponding timestamps would be converted to
time zone of the TimeSeries.
Time zone for time. Corresponding timestamps would be converted
to time zone of the DatetimeIndex.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to time zone of the DatetimeIndex -> to this time zone.


See Also
--------
tz_localize : Localize tz-naive DatetimeIndex to given time zone,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to a given time zone

or remove timezone from a tz-aware DatetimeIndex

hammadmashkoor and others added 3 commits March 13, 2018 15:52
Formatting on See Also

Added tz

PEP8 on example

Switched to tz_convert
@TomAugspurger
Copy link
Contributor

Made a few changes. Biggest one being changing the first example to demonstrate tz_convert instead of tz_localize.

@TomAugspurger TomAugspurger merged commit 1837929 into pandas-dev:master Mar 13, 2018
@TomAugspurger
Copy link
Contributor

Thanks @hammadmashkoor !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Timezones Timezone data dtype
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants